﻿Option Explicit On
Option Strict On
Imports System.Globalization

Namespace Framework
    Public NotInheritable Class CLASSDATE

        Public Shared DTINFO As DateTimeFormatInfo = DateTimeFormatInfo.InvariantInfo

        '-----------ดึง  วันที่  จาก  SERVER -------------------------

        Public Shared Function GETDATEATSERVER() As DateTime
            Try
                SQLConnections.SqlConnectionOpen()
                SQLConnections.Cmd = SQLConnections.Cnn.CreateCommand
                With SQLConnections.Cmd
                    .CommandType = CommandType.Text
                    .CommandText = "Select GetDate()"
                    Return CDate(.ExecuteScalar)
                End With

                SQLConnections.DisposeSqlConnection(SQLConnections.Cmd)
            Catch ex As Exception
                SQLConnections.DisposeSqlConnection(SQLConnections.Cmd)
                MsgBox(ex.Message())
            End Try
        End Function

        Public Shared Function FORMATDATE(ByVal DataDate As DateTime) As String
            'Return DataDate.ToString("d", DTINFO)
            Return Format(DataDate, "dd MMM yyyy")
        End Function

        Public Shared Function FORMATTIME(ByVal DataDate As DateTime) As String
            Return Format(DataDate, "h:mm tt")
        End Function

        Public Shared Function GETYEAR_2_POSITION(ByVal DataDate As DateTime) As String
            Return GetChar(FORMATDATE(DataDate), 9) & GetChar(FORMATDATE(DataDate), 10)
        End Function

        Public Shared Function GETYEAR_4_POSITION(ByVal DataDate As DateTime) As String
            Return GetChar(FORMATDATE(DataDate), 7) & GetChar(FORMATDATE(DataDate), 8) & GetChar(FORMATDATE(DataDate), 9) & GetChar(FORMATDATE(DataDate), 10)
        End Function

        Public Shared Function GETMONTH(ByVal DataDate As DateTime) As String
            Return GetChar(FORMATDATE(DataDate), 1) & GetChar(FORMATDATE(DataDate), 2)
        End Function

        Public Shared Function GETDAY(ByVal DataDate As DateTime) As String
            Return GetChar(FORMATDATE(DataDate), 4) & GetChar(FORMATDATE(DataDate), 5)
        End Function
    End Class
End Namespace
